/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.Labels;

import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.Mapper;


public class SeeAlsos extends AbstractComponent {
	

	public static final String CSSIDFIELD = "divseealsos";
	public static final String CSSIDCONTENT = "divseealsoscontent";
	public static final String NAMEFORMELEMENT = "seeAlsoEntry";
	
	public static final String FOCUSIDOFEDITPANEL = NAMEFORMELEMENT + "_sel";

	public static String getEmptyFieldContainer() {

		String ret = AbstractComponent.getEmptyFieldContainer( CSSIDFIELD );
		
		return ret;
	}
	
	public static String getLinkToShowSecondaryFieldInEditMode( boolean shownavigationInEditField ) {
				
		return getLinkToShowSecondaryFieldInEditMode(CSSIDFIELD, shownavigationInEditField, Labels.SEEALSO.getNamePlural() );
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode(  URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		ret += getTitleContainer( model.Labels.SEEALSO, false, true );
		ret += getFormContainer( getFormInViewMode( uri, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		String ret = "";
		
		ret += getContentInEditMode(uri, entry, exceptions, showNavigation);

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}

	public static String getContentInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		String ret = "";
		
		ret += getTitleContainer( model.Labels.SEEALSO, false, true );
		ret += getFormContainer( getFormInEditMode( uri, entry, exceptions, showNavigation ), CSSIDCONTENT );
		
		return ret;
	}
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";

		java.util.ArrayList<URI> list = Mapper.selectSeeAlso( uri );
		String content = getSeeAlsoHTMLList(list);		

		ret = renderViewForm( 
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT, 
				showNavigation,
				showNavigationInEditMode );
		
		return ret;
	}
	
	private static String getSeeAlsoHTMLList( java.util.ArrayList<URI> entry ) {
		
		String ret = "";
		
		if( entry==null ) {
			ret += NOTHINGSELECTED;
		}
		else {

			for (int i=0; i<entry.size(); i++) {
				ret += 
					"<span id=\"" + NAMEFORMELEMENT + i + "\">" + 
					controller.HelperFunctions.getLinkedURI( entry.get(i).stringValue() ) + 
					"</span>\n";	
			}

			ret += "<script type=\"text/javascript\">\n";
			for (int i=0; i<entry.size(); i++) {
				
				ret += "checkIfURIIsReachable('" + NAMEFORMELEMENT + i + "','"+ entry.get(i).stringValue() +"','"+ model.DHTMLConstants.CHECKIFURLISREACHABLE +"');\n";			
			}
			ret += "</script>\n";
		}
		
		return ret;	
	}
	

	
	public static String getFormInEditMode( URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		
		if( uri!=null) {

			ArrayList<URI> tmp = Mapper.selectSeeAlso( uri );
			
			if( tmp!=null ) {
				actualVersionContent = getSeeAlsoHTMLList( tmp );
			}
		}
		
		if(entry==null)
			entry="";
		
		

		
		// #########################################
		
		String selectedContent = 
			"	<div id=\"" + NAMEFORMELEMENT + "_act\" >"; // WITHOUT "\n" !!!
	
		// the actual values	
		ArrayList<String> strarray = HelperFunctions.extractStrings(entry);
		if( strarray!=null ) {
			for( int i = 0; i<strarray.size(); i++ ){

				selectedContent += 
					getSelectedElementHTML( strarray.get(i), strarray.get(i), "", NAMEFORMELEMENT+ "_act", NAMEFORMELEMENT + "_deletebuttonid" ) + " ";
			}
		}
		
				
		selectedContent +=
			"	</div>\n";

		// #########################################
		// #########################################
		
		
		String addContent = 
			"	<div>\n";

		addContent += getDeleteButton(NAMEFORMELEMENT);

		addContent +=
			"		<div class=\"addcontainer\">\n";
		addContent += 
			"			<input tabindex=\"\" id=\"" + NAMEFORMELEMENT + "_sel\" name=\"" + NAMEFORMELEMENT + "_sel\" value=\"http://\" type=\"text\" class=\"rightselectbox\" " +
				" onkeypress=\"if((event.keyCode==13)){document.getElementById('"+ NAMEFORMELEMENT +"_addbutton').click();}\" " +
				"onblur=\"onkeyup();\" onkeyup=\"if( validateSeeAlsoURI(this,document.getElementById('" + NAMEFORMELEMENT + "_errors'))==true) document.getElementById('" + NAMEFORMELEMENT + "_addbutton').disabled=false; else document.getElementById('" + NAMEFORMELEMENT + "_addbutton').disabled=true; \"" +
				">\n";
		addContent +=
			"			<input style=\"\" disabled=\"true\" id=\""+ NAMEFORMELEMENT +"_addbutton\" type=\"button\" onClick=\"addSeeAlso('"+ NAMEFORMELEMENT +"','http://');this.disabled='true';\" value=\"add\" />";
		addContent += 
			"			<div class=\"inputhint\">e.g. http://en.wikipedia.org/wiki/Regular_expression</div>";
		addContent += 
			"		</div>\n";

		
		addContent +=
			"	</div>\n";
		
		
		addContent += 
			"	<div class=\"clearer\">&nbsp;</div>\n";

			// #########################################
			// #########################################

		
		String content = selectedContent + addContent;
		
	
		// the actual values (again; this field is filled / decreased with DHTML but has to be "initialized")
		content +=
			"<input type=\"hidden\" name=\"" + NAMEFORMELEMENT + "\" value=\"" + entry + "\" />\n";

		ret = renderEditForm(
			Labels.SEEALSO,
			content,
			exceptions, 
			actualVersionContent, 
			actualTimestamp,
			CSSIDCONTENT, 
			NAMEFORMELEMENT, 
			showNavigation );


		return ret;
	}	

}
